package cn.fl.fgi.controller;


import cmm.mid.core.framework.page.DataResultVO;
import cmm.mid.core.framework.page.ListPageVO;
import cmm.mid.core.framework.page.Pager;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.DateUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.fgi.api.condition.FgiFunCfgQueryCondition;
import cn.fl.fgi.service.IFgiFunCfgService;
import cn.fl.fgi.vo.FgiFunCfgVO;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.ref.fncprjbscinfo.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author caolei
 * @since 2020-05-21
 */
@Controller
@RequestMapping("/fgifuncfg/fgiFunCfg")
public class FgiFunCfgController {

    @Autowired
    private IFgiFunCfgService fgiFunCfgService ;
    @RequestMapping(value = "/toListPage.do", method = {RequestMethod.GET})
    public ModelAndView toListPage(HttpServletRequest request, HttpServletResponse response) {
        ModelAndView view = new ModelAndView("fgi/fgifuncfg/fgiFunCfgListPage");
        return view ;
    }

    @RequestMapping(value = "/initList.do", method = {RequestMethod.GET})
    @ResponseBody
    public Object initList(HttpServletRequest request, HttpServletResponse response) {
        String page = request.getParameter("page");
        String pageSize = request.getParameter("pageSize");
        String funNm = request.getParameter("funNm");
        String upTyp = request.getParameter("upTyp");
        String payTmStart = request.getParameter("payTmStart");
        String payTmEnd = request.getParameter("payTmEnd");
        String overDueDaysStart = request.getParameter("overDueDaysStart");
        String overDueDaysEnd = request.getParameter("overDueDaysEnd");

        FgiFunCfgQueryCondition condition = new FgiFunCfgQueryCondition();
        condition.setFunNm(funNm);
        if(StringUtils.isNotEmpty(upTyp)){
            condition.setUpTyp(Integer.valueOf(upTyp));
        }
        if(StringUtils.isNotEmpty(payTmStart)){
            condition.setPayTmStart(DateUtils.formatStringToDate(payTmStart,DateUtils.YYYY_MM_DD));
        }
        if(StringUtils.isNotEmpty(payTmEnd)){
            condition.setPayTmEnd(DateUtils.formatStringToDate(payTmEnd,DateUtils.YYYY_MM_DD));
        }
        if(StringUtils.isNotEmpty(overDueDaysStart)){
            condition.setOverdueDaysStart(overDueDaysStart);
        }
        if(StringUtils.isNotEmpty(overDueDaysEnd)){
            condition.setOverdueDaysEnd(overDueDaysEnd);
        }
        if(StringUtils.isEmpty(page)){
            page = "1";
        }
        if(StringUtils.isEmpty(pageSize)){
            pageSize = "10";
        }
        Pager pager = this.fgiFunCfgService.searchListPage(condition,Integer.valueOf(page),Integer.valueOf(pageSize));
        List<FgiFunCfgVO> vList = (List<FgiFunCfgVO>)pager.getResultList();
        StringBuffer sb = null;
        if(CollectionUtils.isNotEmpty(vList)){
            for (FgiFunCfgVO vo :vList) {
                if(vo.getPayTmStart() != null && vo.getPayTmEnd() != null){
                    sb = new StringBuffer();
                    sb.append(DateUtils.formatDateToString(vo.getPayTmStart(),DateUtils.YYYY_MM_DD));
                    sb.append("—");
                    sb.append(DateUtils.formatDateToString(vo.getPayTmEnd(),DateUtils.YYYY_MM_DD));
                    vo.setPayTm(sb.toString());
                }
                if(StringUtils.isNotEmpty(vo.getOverdueDaysStart()) && StringUtils.isNotEmpty(vo.getOverdueDaysEnd())){
                    sb = new StringBuffer();
                    sb.append(vo.getOverdueDaysStart());
                    sb.append("—");
                    sb.append(vo.getOverdueDaysEnd());
                    vo.setOverdueDays(sb.toString());
                }
            }
        }
        ListPageVO listPageVO = new ListPageVO<FgiFunCfgVO>();
        listPageVO.setRows(vList);
        listPageVO.setTotal(pager.getTotalCount());
        return listPageVO ;
    }

    @RequestMapping(value = "/toAddPage.do", method = {RequestMethod.GET})
    public ModelAndView toAddPage(HttpServletRequest request, HttpServletResponse response) {
        ModelAndView view = new ModelAndView("fgi/fgifuncfg/fgiFunCfgAddPage");
        view.addObject("crtTm",DateUtils.formatDateToString(new Date(),DateUtils.YYYY_MM_DDHHMM));
        view.addObject("crtUsrId",CurrentThreadContext.getCurrentUserId());
        view.addObject("crtUsrNm",CurrentThreadContext.getCurrentUserName());
        return view ;
    }

    @RequestMapping(value = "/add.do", method = {RequestMethod.GET})
    @ResponseBody
    public Object add(HttpServletRequest request, HttpServletResponse response) {

        DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES,"新增成功");
        String crtTm = request.getParameter("crtTm");
        String crtUsrId = request.getParameter("crtUsrId");
        String crtUsrNm = request.getParameter("crtUsrNm");
        String fundRasInsNm = request.getParameter("fundRasInsNm");
        String overDueDaysStart = request.getParameter("overDueDaysStart");
        String overDueDaysEnd = request.getParameter("overDueDaysEnd");
        String payTmStart = request.getParameter("payTmStart");
        String payTmEnd = request.getParameter("payTmEnd");
        String upTyp = request.getParameter("upTyp");
        String funId = request.getParameter("funId");

        if(StringUtils.isEmpty(fundRasInsNm)){
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo("金融机构名称为空，新增失败！");
            return result ;
        }
        if(StringUtils.isEmpty(upTyp)){
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo("上报类型为空，新增失败！");
            return result ;
        }
        if(StringUtils.isEmpty(funId)){
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo("金融机构ID为空，新增失败！");
            return result ;
        }

        if("2".equals(upTyp) && (StringUtils.isEmpty(overDueDaysStart) || StringUtils.isEmpty(overDueDaysEnd))){
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo("逾期天数为空，新增失败！");
            return result ;
        }

        if("1".equals(upTyp) && (StringUtils.isEmpty(payTmStart) || StringUtils.isEmpty(payTmEnd))){
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo("金融机构放款时间为空，新增失败！");
            return result ;
        }
        if((StringUtils.isNotEmpty(payTmStart) || StringUtils.isNotEmpty(payTmEnd)) &&
                (StringUtils.isNotEmpty(overDueDaysStart) || StringUtils.isNotEmpty(overDueDaysEnd))){
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo("金融机构放款时间与逾期天数不能同时存在，新增失败！");
            return result ;
        }
        try {
            FgiFunCfgVO vo = new FgiFunCfgVO();
            if(StringUtils.isEmpty(crtTm)){
                vo.setCrtTm(new Date());
            }else{
                vo.setCrtTm(DateUtils.formatStringToDate(crtTm,DateUtils.YYYY_MM_DD));
            }
            if(StringUtils.isEmpty(crtUsrId)){
                vo.setCrtUsrId(CurrentThreadContext.getCurrentUserId());
            }else{
                vo.setCrtUsrId(Long.valueOf(crtUsrId));
            }
            if(StringUtils.isEmpty(crtUsrNm)){
                vo.setCrtUsrNm(CurrentThreadContext.getCurrentUserName());
            }else{
                vo.setCrtUsrNm(crtUsrNm);
            }
            vo.setFunNm(fundRasInsNm);
            vo.setFunId(Long.valueOf(funId));
            vo.setOverdueDaysStart(overDueDaysStart);
            vo.setOverdueDaysEnd(overDueDaysEnd);
            if(StringUtils.isNotEmpty(payTmStart)){
                vo.setPayTmStart(DateUtils.formatStringToDate(payTmStart,DateUtils.YYYY_MM_DD));
            }
            if(StringUtils.isNotEmpty(payTmEnd)){
                vo.setPayTmEnd(DateUtils.formatStringToDate(payTmEnd,DateUtils.YYYY_MM_DD));
            }
            vo.setUpTyp(Integer.valueOf(upTyp));
            vo.setIsDel(BaseConstant.IS_YESNO_NO);
            vo.setIsValid(BaseConstant.IS_YESNO_YES);
            vo.setIsNew(BaseConstant.IS_YESNO_YES);
            boolean b = this.fgiFunCfgService.addFgiFunCfg(vo);
            if(!b){
                result.setSuccess(BaseConstant.IS_YESNO_NO);
                result.setInfo("新增失败！");
            }
        }catch (BaseException be){
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo("新增失败，系统错误！");
            return result;
        }catch (Exception e){
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo("新增失败，系统错误！");
            return result;
        }

        return result ;
    }
}
